<?php
/**
* Tweet Display Back Module for Joomla!
*
* @version		$Id: helper.php 128 2011-03-06 11:39:17Z mbabker $
* @copyright	Copyright (C) 2010-2011 Michael Babker. All rights reserved.
* @license		GNU/GPL - http://www.gnu.org/copyleft/gpl.html
*/

// No direct access
defined('_JEXEC') or die;

class modTweetDisplayBackHelper {

	/**
	 * Function to fetch a JSON feed
	 * 
	 * @param	string	$req	The URL of the feed to load
	 * @return	array	$obj	The fetched JSON query
	 * @since	1.0.7
	 */
	static function getJSON($req) {
		// Create a new CURL resource
		$ch = curl_init($req);
		
		// Set options
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		
		// Grab URL and pass it to the browser and store it as $json
		$json = curl_exec($ch);
		
		// Close CURL resource
		curl_close($ch);
		
		// Decode the fetched JSON
		$obj = json_decode($json, true);
		
		if (isset($obj['error'])) return false;
		
		return $obj;		
	}
	
	/**
	 * Function to get the rate limit of a Twitter user
	 * 
	 * @param	string	$params
	 * @return	string	$hits	The number of remaining hits on a user's rate limit
	 * @since	1.0.6
	 */
	static function getLimit($params) {
		// Load the parameters
		$uname = $params->get("twitterName","");
		$req = "http://api.twitter.com/1/account/rate_limit_status.json?screen_name=".$uname."";
		
		// Fetch the decoded JSON
		$obj = self::getJSON($req);
				
		// Get the remaining hits count
		if (isset ($obj['remaining_hits'])) { 
		 	$hits = $obj['remaining_hits'];
		} else {
		 	return false;
		}
		return $hits;
	}

	/**
	 * Function to load a user's user_timeline Twitter feed
	 * 
	 * @param	string	$params
	 * @return	object	$twitter	A formatted object with the user's tweets
	 * @since	1.0.0
	 */
	static function getTweets($params) {
		// Load the parameters
		$uname = $params->get("twitterName","");
		$count = $params->get("twitterCount",3);
		$retweet = $params->get("tweetRetweets",1);
		
		// Determine whether to pull retweets or not
		if ($retweet == 1) {
			$req = "http://api.twitter.com/1/statuses/user_timeline.json?count=".$count."&include_rts=1&screen_name=".$uname."";
		} else {
			$req = "http://api.twitter.com/1/statuses/user_timeline.json?count=".$count."&screen_name=".$uname."";
		}
		
		// Fetch the decoded JSON
		$obj = self::getJSON($req);
		
		// Render the JSON into a formatted array
		$twitter = self::renderTwitter($obj, $params);
		return $twitter;
	}
		
	/**
	 * Function to render the user timeline JSON into HTML
	 * 
	 * @param	array	$obj		The decoded JSON feed
	 * @param	string	$params
	 * @return	object	$twitter	The formatted object for display
	 * @since	1.0.0
	 */
	static function renderTwitter($obj, $params) {
		// Initialize
		$twitter = array();
		
		// Set variables
		$tweetName		= $params->get("tweetName", 1);
		$tweetAlignment	= $params->get("tweetAlignment", 'left');
		$tweetReply		= $params->get("tweetReply", 1);
		$tweetRTCount	= $params->get("tweetRetweetCount", 1);

		// Check the first object for user info
		if (isset($obj[0])) {
			$userInfo = $obj[0];
		} else {
		 	return false;
		}
		// Header info
		$headerUser = '';
		if ($params->get("headerUser", 1)==1) {
			if ($params->get("headerName", 1)==1) {
				$headerUser = "<a href=\"http://twitter.com/".$userInfo['user']['screen_name']."\">".$userInfo['user']['name']."</a>";
			}
			else {
				$headerUser = "<a href=\"http://twitter.com/".$userInfo['user']['screen_name']."\">".$userInfo['user']['screen_name']."</a>";
			}
		}
		$headerBio = '';
		if ($params->get("headerBio", 1)==1) {
			$headerBio = $userInfo['user']['description'];
		}
		$headerLocation = '';
		if ($params->get("headerLocation", 1)==1) {
			$headerLocation = $userInfo['user']['location'];
		}
		$headerWeb = '';
		if ($params->get("headerWeb", 1)==1) {
			$headerWeb = "<a href=\"".$userInfo['user']['url']."\">".$userInfo['user']['url']."</a>";
		}
		$headerAvatar = "<img src=\"http://api.twitter.com/1/users/profile_image/twitter.json?screen_name=".$userInfo['user']['screen_name']."&size=bigger\" width=\"73px\" alt=\"".$userInfo['user']['screen_name']."\" />";
		
		// Footer info
		
		// If a "Follow me" link is displayed, determine whether to display a button or text
		// followType 1 is image, 0 is text
		$footerFollowMe = '';
		if ($params->get("footerFollowLink", 1) == 1) {
			if ($params->get("footerFollowType", 1) == 1) {
				$footerFollowMe = "<div class=\"TDB-footer-follow-img\"><b><a href=\"http://twitter.com/".$userInfo['user']['screen_name']."\" rel=\"nofollow\"><img src=\"http://twitter-badges.s3.amazonaws.com/".$params->get('footerFollowImgMeUs')."-".$params->get('footerFollowImg').".png\" alt=\"Follow ".$userInfo['user']['screen_name']." on Twitter\" align=\"center\" /></a></b></div>";
			} else {
				$footerFollowMe = "<hr /><div class=\"TDB-footer-follow-link\"><b><a href=\"http://twitter.com/".$userInfo['user']['screen_name']."\" rel=\"nofollow\">".$params->get('footerFollowText', 'Follow me on Twitter')."</a></b></div>";
			}
		}
		$footerPoweredBy = '';
		if ($params->get("footerPoweredBy", 1) == 1) {
			//Check the type of link to determine the appropriate opening tags
			if ($params->get("footerFollowType", 1) == 1) {
				$footerPoweredBy = "<div class=\"TDB-footer-powered-img\">";
			} else {
				$footerPoweredBy = "<hr /><div class=\"TDB-footer-powered-text\">";
			}
			$footerPoweredBy .= "Powered by <a href=\"http://www.flbab.com/extensions/tweet-display-back/13-info\">Tweet Display Back</a></div>";
		}
		
		// Tweets
		$i = 0;
		foreach ($obj as $o) {
			// Header Information
			$twitter[$i]->header->user = $headerUser;
			$twitter[$i]->header->bio = $headerBio;
			$twitter[$i]->header->location = $headerLocation;
			$twitter[$i]->header->web = $headerWeb;
			$twitter[$i]->header->avatar = $headerAvatar;
			
			// Footer Information
			$twitter[$i]->footer->follow_me = $footerFollowMe;
			$twitter[$i]->footer->powered_by = $footerPoweredBy;

			// Check if the item is a retweet, and if so gather data from the retweeted_status datapoint
			if(isset($o['retweeted_status'])) {
				// Retweeted user
				if ($tweetName == 1) {
					$twitter[$i]->tweet->user = "<b><a href=\"http://twitter.com/".$o['retweeted_status']['user']['screen_name']."\">".$o['retweeted_status']['user']['screen_name']."</a>".$params->get("tweetUserSeparator")."</b> ";
				}
				$twitter[$i]->tweet->created = "Retweeted ";
				$twitter[$i]->tweet->avatar = "<img align=\"".$tweetAlignment."\" alt=\"".$o['retweeted_status']['user']['screen_name']."\" src=\"".$o['retweeted_status']['user']['profile_image_url']."\" width=\"32px\"/>";
				$twitter[$i]->tweet->text = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $o['retweeted_status']['text']);
			} else {
				// User
				if ($tweetName == 1) {
					$twitter[$i]->tweet->user = "<b><a href=\"http://twitter.com/".$o['user']['screen_name']."\">".$o['user']['screen_name']."</a>".$params->get("tweetUserSeparator")."</b> ";
				}
				$twitter[$i]->tweet->avatar = "<img align=\"".$tweetAlignment."\" alt=\"".$o['user']['screen_name']."\" src=\"".$o['user']['profile_image_url']."\" width=\"32px\"/>";
				$twitter[$i]->tweet->text = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $o['text']);
			}
			// Info below is specific to each tweet, so it isn't checked against a retweet
			// Determine whether to display the time as a relative or static time
			if ($params->get("tweetCreated", 1)==1) {
				if ($params->get("tweetRelativeTime", 1) == 1) {
					$twitter[$i]->tweet->created .= "<a href=\"http://twitter.com/".$o['user']['screen_name']."/status/".$o['id_str']."\">".self::renderRelativeTime($o['created_at'])."</a>";
				}
				else {
					$twitter[$i]->tweet->created .= "<a href=\"http://twitter.com/".$o['user']['screen_name']."/status/".$o['id_str']."\">".JHTML::date($o['created_at'])."</a>";
				}
			}
			// Display the tweet source
			if (($params->get("tweetSource", 1) == 1)) {
				$twitter[$i]->tweet->created .= " via ".$o['source'];
			}
			// Display the location the tweet was made from
			if (($params->get("tweetLocation", 1) == 1) && ($o['place']['full_name'])) {
				$twitter[$i]->tweet->created .= " from <a href=\"http://maps.google.com/maps?q=".$o['place']['full_name']."\" target=\"_blank\">".$o['place']['full_name']."</a>";
			}
			// If the tweet is a reply, display a link to the tweet it's in reply to
			if (($o['in_reply_to_screen_name']) && ($o['in_reply_to_status_id_str'])) {
				$twitter[$i]->tweet->created .= " in reply to <a href=\"http://twitter.com/".$o['in_reply_to_screen_name']."/status/".$o['in_reply_to_status_id_str']."\">".$o['in_reply_to_screen_name']."</a>";
			}
			if (($tweetReply == 1) || (($tweetRTCount == 1) && ($o['retweet_count'] >= 1))) {
				$twitter[$i]->tweet->created .= " &bull; ";
			}
			// Display a reply link
			if ($tweetReply == 1) {
				$twitter[$i]->tweet->created .= "<a href=\"http://twitter.com/?status=@".$o['user']['screen_name']." &in_reply_to_status_id=".$o['id_str']."&in_reply_to=".$o['user']['screen_name']."\" target=\"_blank\">".JText::_('MOD_TWEETDISPLAYBACK_REPLY')."</a>";
			}
			if (($tweetReply == 1) && (($tweetRTCount == 1) && ($o['retweet_count'] >= 1))) {
				$twitter[$i]->tweet->created .= " &bull; ";
			}
			// Display the number of times the tweet has been retweeted
			if (($tweetRTCount == 1) && ($o['retweet_count'] >= 1)) {
				$twitter[$i]->tweet->created .= JText::plural('MOD_TWEETDISPLAYBACK_RETWEETS', $o['retweet_count']);
			}
			// If set, convert user and hash tags into links
			if ($params->get("tweetLinks", 1) == 1) {
				$twitter[$i]->tweet->text = preg_replace("/@(\w+)/", "@<a class=\"userlink\" href=\"http://twitter.com/\\1\" target=\"_blank\">\\1</a>", $twitter[$i]->tweet->text);
				$twitter[$i]->tweet->text = preg_replace("/#(\w+)/", "#<a class=\"hashlink\" href=\"http://twitter.com/search?q=\\1\" target=\"_blank\">\\1</a>", $twitter[$i]->tweet->text);
			}
			$i++;
		}
		return $twitter;
	}
	
	/**
	 * Function to convert a static time into a relative measurement
	 * 
	 * @param	string	$date	The date to convert
	 * @return	string	$date	A text string of a relative time
	 * @since	1.0.0
	 */
	static function renderRelativeTime($date) {
		$diff = time() - strtotime($date);
		// Less than a minute
		if ($diff < 60) {
			return JText::_('MOD_TWEETDISPLAYBACK_CREATE_LESSTHANAMINUTE');
		}
		$diff = round($diff/60);
		// 1 to 59 minutes
		if ($diff < 60) {
			return JText::plural('MOD_TWEETDISPLAYBACK_CREATE_MINUTES', $diff);
		}
		$diff = round($diff/60);
		// 1 to 23 hours
		if ($diff < 24) {
			return JText::plural('MOD_TWEETDISPLAYBACK_CREATE_HOURS', $diff);
		}
		$diff = round($diff/24);
		// 1 to 6 days
		if ($diff < 7) {
			return JText::plural('MOD_TWEETDISPLAYBACK_CREATE_DAYS', $diff);
		}
		$diff = round($diff/7);
		// 1 to 3 weeks
		if ($diff < 4) {
			return JText::plural('MOD_TWEETDISPLAYBACK_CREATE_WEEKS', $diff);
		}
		// If older than 4 weeks, display a static time
		return JHTML::date($date);	
	}
}
